//
//  NetworkCenter.m
//  zhangjiantest2
//
//  Created by ZJL on 2024/7/5.
//

#import "NetworkCenter.h"
#define BASEURL  @"http://127.0.0.1:21325"
#include <stdlib.h>
#include <string.h>
#import "HexConverter.h"
#include <unistd.h>
#import <Security/Security.h>

#import <Foundation/Foundation.h>
#import "MessagesManagement.pbobjc.h"
struct Features;
struct  EntropyAck;
struct PinMatrixAck;

@implementation NetworkCenter

static NetworkCenter *_sharedInstance = nil;

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [[self alloc] init];
    });
    return _sharedInstance;
}

// 获取版本号
+(void)checkBridgeVersionWithBody:(NSData *)data 
                       completion:(completion)completion
{
    [NetworkManager sendRequestWithURL:[NSURL URLWithString:BASEURL] body:nil completion:^(NSData * _Nonnull data, NSError * _Nonnull error,NSURLResponse *response) {
        completion(data,error,response);
    }];
}

+(void)enumerateDevicesWithBody:(NSData *)data
                     completion:(completion)completion
{
    [NetworkManager sendRequestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/enumerate",BASEURL]] body:nil completion:^(NSData * _Nonnull data, NSError * _Nonnull error,NSURLResponse *response) {
        completion(data,error,response);
    }];
}

+(void)acquireDeviceWithPathWithBody:(NSData *)data 
                                path:(NSString *)path
                          completion:(completion)completion;
{
    [NetworkManager sendRequestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/acquire/%@/null",BASEURL,path]] body:data completion:^(NSData * _Nonnull data, NSError * _Nonnull error,NSURLResponse *response) {
        completion(data,error,response);
    }];
}

+(void)sendRecoveryCommandWithSessionWithBody:(NSData *)data 
                                      session:(NSString *)session
                                   completion:(completion)completion;
{
    [NetworkManager sendRequestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/call/%@",BASEURL,session]] body:data completion:^(NSData * _Nonnull data, NSError * _Nonnull error,NSURLResponse *response) {
        completion(data,error,response);
    }];
}

+(void)sendButtonAckCommandWithSessionWithBody:(NSData *)data 
                                       session:(NSString *)session
                                    completion:(completion)completion;
{
    [NetworkManager sendRequestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/call/%@",BASEURL,session]] body:data completion:^(NSData * _Nonnull data, NSError * _Nonnull error,NSURLResponse *response) {
        completion(data,error,response);
    }];
}

+(void)listenDeviceWithPathWithBody:(NSData *)data 
                         completion:(completion)completion;
{
    [NetworkManager sendRequestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/listen",BASEURL]] body:data completion:^(NSData * _Nonnull data, NSError * _Nonnull error,NSURLResponse *response) {
        completion(data,error,response);
    }];
}

+(void)sendRestartCommandWithSessionWithBody:(NSData *)data 
                                     session:(NSString *)session
                                  completion:(completion)completion;
{
    [NetworkManager sendRequestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/call/%@",BASEURL,session]] body:data completion:^(NSData * _Nonnull data, NSError * _Nonnull error,NSURLResponse *response) {
        completion(data,error,response);
    }];
}

+(void)getDeviceStatusWithSessionWithBody:(NSData *)data 
                                  session:(NSString *)session
                               completion:(completion)completion;
{
    [NetworkManager sendRequestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/call/%@",BASEURL,session]] body:data completion:^(NSData * _Nonnull data, NSError * _Nonnull error,NSURLResponse *response) {
        completion(data,error,response);
    }];
}

+(void)getFeaturesWithSessionWithBody:(NSData *)data 
                              session:(NSString *)session
                           completion:(completion)completion;
{
    [NetworkManager sendRequestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/call/%@",BASEURL,session]] body:data completion:^(NSData * _Nonnull data, NSError * _Nonnull error,NSURLResponse *response) {
        completion(data,error,response);
    }];
}

+(void)sendResetDeviceCommandWithSessionWithBody:(NSData *)data 
                                         session:(NSString *)session
                                      completion:(completion)completion;
{
    [NetworkManager sendRequestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/call/%@",BASEURL,session]] body:data completion:^(NSData * _Nonnull data, NSError * _Nonnull error,NSURLResponse *response) {
        completion(data,error,response);
    }];
}

+(void)startRecoveryWithSessionWithBody:(NSData *)data
                                session:(NSString *)session
                             completion:(completion)completion;
{
    [NetworkManager sendRequestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/call/%@",BASEURL,session]] body:data completion:^(NSData * _Nonnull data, NSError * _Nonnull error,NSURLResponse *response) {
        completion(data,error,response);
    }];
}

+(void)sendWordWithBody:(NSData *)data 
                session:(NSString *)session
             completion:(completion)completion;
{
    [NetworkManager sendRequestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/call/%@",BASEURL,session]] body:data completion:^(NSData * _Nonnull data, NSError * _Nonnull error,NSURLResponse *response) {
        completion(data,error,response);
    }];
}

+(void)startBackupDeviceCommandWithSessionWithBody:(NSData *)data 
                                           session:(NSString *)session
                                        completion:(completion)completion;
{
    [NetworkManager sendRequestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/call/%@",BASEURL,session]] body:data completion:^(NSData * _Nonnull data, NSError * _Nonnull error,NSURLResponse *response) {
        completion(data,error,response);
    }];
}

+(void)sendEntropyAckCommandWithSessionWithBody:(NSData *)data
                                        session:(NSString *)session
                                     completion:(completion)completion
{
    [NetworkManager sendRequestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/call/%@",BASEURL,session]] body:data completion:^(NSData * _Nonnull data, NSError * _Nonnull error,NSURLResponse *response) {
        completion(data,error,response);
    }];
}

+(void)getFeaturesWithSessionForCreateWithBody:(NSData *)data
                                       session:(NSString *)session
                                    completion:(completion)completion
{
    [NetworkManager sendRequestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/call/%@",BASEURL,session]] body:data completion:^(NSData * _Nonnull data, NSError * _Nonnull error,NSURLResponse *response) {
        completion(data,error,response);
    }];
}

+(void)getFeaturesWithSessionForPinWithBody:(NSData *)data
                                    session:(NSString *)session
                                 completion:(completion)completion
{
    [NetworkManager sendRequestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/call/%@",BASEURL,session]] body:data completion:^(NSData * _Nonnull data, NSError * _Nonnull error,NSURLResponse *response) {
        completion(data,error,response);
    }];
}

// get Address
+(void)getAddressWithSessionWithBody:(NSData *)data
                             session:(NSString *)session
                          completion:(completion)completion
{
    [NetworkManager sendRequestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/call/%@",BASEURL,session]] body:data completion:^(NSData * _Nonnull data, NSError * _Nonnull error,NSURLResponse *response) {
        completion(data,error,response);
    }];
}

// get Public key
+(void)getPublicKeyWithSessionWithBody:(NSData *)data
                               session:(NSString *)session
                            completion:(completion)completion
{
    [NetworkManager sendRequestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/call/%@",BASEURL,session]] body:data completion:^(NSData * _Nonnull data, NSError * _Nonnull error,NSURLResponse *response) {
        completion(data,error,response);
    }];
}

// create wallet
-(void)createWallet:(Get_Wallet_Type)type
{
    [NetworkCenter checkBridgeVersionWithBody:nil completion:^(NSData *data, NSError *error, NSURLResponse *response) {
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
        if (httpResponse.statusCode == 200) {
            NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            NSLog(@"Status Response: %@", jsonResponse);
            NSLog(@"Bridge Version: %@", jsonResponse[@"version"]);
            [self enumerateDevice:type];
        } else {
            NSLog(@"Failed to check bridge version. Status code: %ld", (long)httpResponse.statusCode);
        }
    }];
}

// recovery wallet
-(void)recoveryWallet:(Get_Wallet_Type)type
{
    [NetworkCenter checkBridgeVersionWithBody:nil completion:^(NSData *data, NSError *error, NSURLResponse *response) {
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
        if (httpResponse.statusCode == 200) {
            NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            NSLog(@"Status Response: %@", jsonResponse);
            NSLog(@"Bridge Version: %@", jsonResponse[@"version"]);
            [self enumerateDevice:type];
        } else {
            NSLog(@"Failed to check bridge version. Status code: %ld", (long)httpResponse.statusCode);
        }
    }];
}

- (void)enumerateDevice:(Get_Wallet_Type)type
{
    [NetworkCenter enumerateDevicesWithBody:nil completion:^(NSData *data, NSError *error, NSURLResponse *response) {
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
        if (httpResponse.statusCode == 200) {
            NSArray *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            NSLog(@"Enumerate Response: %@", jsonResponse);
            if (jsonResponse.count > 0) {
                NSString *path = jsonResponse[0][@"path"];
                //[self listenDeviceWithPath:path session:[NSNull null]];
                [self acquireDeviceWithPath:path type:type];
            } else {
                NSLog(@"No devices found.");
            }
        } else {
            NSLog(@"Failed to enumerate devices. Status code: %ld", (long)httpResponse.statusCode);
        }
    }];
}

- (void )acquireDeviceWithPath:(NSString *)path type:(Get_Wallet_Type)type{
    [NetworkCenter acquireDeviceWithPathWithBody:nil path:path completion:^(NSData *data, NSError *error, NSURLResponse *response) {
        if (error) {
            NSLog(@"Error acquiring device: %@", error.localizedDescription);
        }
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
        if (httpResponse.statusCode == 200) {
            NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            NSLog(@"Acquire Response: %@", jsonResponse);
            
            NSString *newSession = jsonResponse[@"session"];
            //[self sendRecoveryCommandWithSession:newSession];
            //[self getDeviceStatusWithSession:newSession];
            //[self getFeaturesWithSession:newSession];
            //[self startRecoveryWithSession:newSession mnemonicLength:12];
            //[self sendInitializeCommandWithSession: newSession];
            //  create wallet
            if(type == Type_Create_Wallet){
                [self sendResetDeviceCommandWithSession:newSession];
            }
            if(type == Type_Recovery_Wallet){
                [self sendRecoveryCommandWithSession:newSession];
            }
            //[self getFeaturesWithSessionForCreate:newSession];
            //[self getFeaturesWithSessionForPin:newSession];
            
        } else {
            NSLog(@"Failed to acquire device. Status code: %ld", (long)httpResponse.statusCode);
        }
    }];
}

- (void)sendRecoveryCommandWithSession:(NSString *)session0
{
    NSString *hexString = @"002d00000010080c10013001400048acf68eb4065000";
    //NSData *bodyData = [HexConverter dataFromHexString:hexString];
    NSData *bodyData = [hexString dataUsingEncoding:NSUTF8StringEncoding];
    [NetworkCenter sendRecoveryCommandWithSessionWithBody:bodyData session:session0 completion:^(NSData *data, NSError *error, NSURLResponse *response) {
        if (error) {
            NSLog(@"Error sending recovery command: %@", error.localizedDescription);
        }
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
        if (httpResponse.statusCode == 200) {
            NSLog(@"Recovery Command Response DATA: %@", data);
            
            NSData *responseData = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            NSLog(@"Recovery Command Response: %@", responseData);
            [self sendButtonAckCommandWithSession:session0];
        } else {
            NSLog(@"Failed to send recovery command. Status code: %ld", (long)httpResponse.statusCode);
        }
    }];
}

- (void)sendResetDeviceCommandWithSession:(NSString *)session0 
{
    NSString *hexString = @"000e0000001a108001180132094d79205472657a6f7238b7ad9eb40640015000"; // ResetDevice command
    NSData *bodyData = [hexString dataUsingEncoding:NSUTF8StringEncoding];
    [NetworkCenter sendResetDeviceCommandWithSessionWithBody:bodyData session:session0 completion:^(NSData *data, NSError *error, NSURLResponse *response) {
        if (error) {
            NSLog(@"Error sending ResetDevice command: %@", error.localizedDescription);
            return;
        }
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
        if (httpResponse.statusCode == 200) {
            NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"ResetDevice Response String: %@", responseString); // This will show the exact content of the response
            NSString *substring = [responseString substringFromIndex:12];

            //NSDictionary *responseData = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            NSLog(@"ResetDevice Command Response: %@", responseString);
            
            // Handle the response, possibly send ButtonAck
            [self handleDeviceResponseString:responseString session: session0];
//            [self listenDeviceWithPath:@"1" session:session0];
            //[self sendButtonAckCommandWithSession:session0];
//            [self sendButtonAckCommandWithSession:session0];
        } else {
            NSLog(@"Failed to send ResetDevice command. Status code: %ld", (long)httpResponse.statusCode);
        }
    }];
}

- (void)handleDeviceResponseString:(NSString *)response session:(NSString *) session {
    NSString *messageType = [response substringToIndex:4];
    if ([messageType isEqualToString:@"0002"]) {
        [self getFeaturesWithSessionForPin:session];
    }else if ([messageType isEqualToString:@"0023"]) {
        [self sendEntropyAckCommandWithSession:session];
    } else if ([messageType isEqualToString:@"001a"]) {
        // Send ButtonAck again if needed
        [self sendButtonAckCommandWithSession:session];
    } else if ([messageType isEqualToString:@"PinMatrixRequest"]) {
        // Handle PinMatrixRequest
        // Prompt the user to enter the PIN and send it using a similar method
    } else if ([messageType isEqualToString:@"PassphraseRequest"]) {
        // Handle PassphraseRequest
        // Prompt the user to enter the passphrase and send it using a similar method
    } else if ([messageType isEqualToString:@"Success"]) {
        // Handle Success
        NSLog(@"Recovery process successful: %@", response);
    } else if ([messageType isEqualToString:@"Failure"]) {
        // Handle Failure
        NSLog(@"Recovery process failed: %@", response);
    } else {
        // Handle other types of messages
        NSLog(@"Unhandled response type: %@", messageType);
    }
}

- (void )sendEntropyAckCommandWithSession:(NSString *)session0 {
    NSString *hexString = @"0024"; // Replace with the actual recovery command
    EntropyAck *entropyAck = [[EntropyAck alloc] init];
    NSData *entropyData = [self generateSecureRandomData: 32];
    [entropyAck setEntropy:entropyData];
    NSData *serializedData = [entropyAck data];
    NSString *hexStringBodyData = [HexConverter hexStringFromData: serializedData];
    NSInteger len = [hexStringBodyData length];
    NSString *hexStringSent = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%@%08x%@",hexString,len / 2,hexStringBodyData]];
    
    NSLog(@"Before sendEntropyAck Command Request hex string: %@", hexStringSent);
    
    NSString *urlString = [NSString stringWithFormat:@"http://127.0.0.1:21325/call/%@", session0];
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/octet-stream" forHTTPHeaderField:@"Content-Type"];
    NSData *bodyData = [hexStringSent dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:bodyData];
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (error) {
            NSLog(@"Error sending ButtonAck command: %@", error.localizedDescription);
        }
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
        if (httpResponse.statusCode == 200) {
            NSLog(@"Post sendEntropyAck Command Response DATA: %@", data);
            NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"Raw sendEntropyAck Response String: %@", responseString); // This will show the exact content of the response
            NSString *substring = [responseString substringFromIndex:12];
            
            
            NSData *responseData = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            NSLog(@"sendEntropyAck Command Response: %@", responseData);
            //[self listenDeviceWithPath:@"1" session:session0];
            [self startBackupDeviceCommandWithSession:session0];
            //[self sendRestartCommandWithSession:session0];
            //[self getFeaturesWithSessionForCreate:session0];
        } else {
            NSLog(@"Failed to send ButtonAck command. Status code: %ld", (long)httpResponse.statusCode);
            //            [self listenDeviceWithPath:@"1" session:session1];
        }
    }];
    
    [dataTask resume ];
}

- (void)startBackupDeviceCommandWithSession:(NSString *)session0 
{
    NSString *hexString = @"002200000000"; // ResetDevice command
    NSData *bodyData = [hexString dataUsingEncoding:NSUTF8StringEncoding];
    [NetworkCenter startBackupDeviceCommandWithSessionWithBody:bodyData session:session0 completion:^(NSData *data, NSError *error, NSURLResponse *response) {
        if (error) {
            NSLog(@"Error sending ResetDevice command: %@", error.localizedDescription);
            return;
        }
        
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
        if (httpResponse.statusCode == 200) {
            
            NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"Raw Response String: %@", responseString); // This will show the exact content of the response
            
            //NSString *substring = [responseData substringFromIndex:12];
            
            
            // Handle the response, possibly send ButtonAck
            [self handleDeviceResponseString:responseString session: session0];
            
        } else {
            NSLog(@"Failed to send ResetDevice command. Status code: %ld", (long)httpResponse.statusCode);
        }
    }];
}

- (void )sendButtonAckCommandWithSession:(NSString *)session0 {
    
    NSString *hexString = @"001b00000000"; // Replace with the actual recovery command
    NSData *bodyData = [hexString dataUsingEncoding:NSUTF8StringEncoding];
    [NetworkCenter sendButtonAckCommandWithSessionWithBody:bodyData session:session0 completion:^(NSData *data, NSError *error, NSURLResponse *response) {
        if (error) {
            NSLog(@"Error sending ButtonAck command: %@", error.localizedDescription);
        }
        
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
        if (httpResponse.statusCode == 200) {
            NSLog(@"Post ButtonAck Command Response DATA: %@", data);
            NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"Raw BtnACK Response String: %@", responseString); // This will show the exact content of the response
            NSString *substring = [responseString substringFromIndex:12];
            NSData *responseData = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            NSLog(@"ButtonAck Command Response: %@", responseData);
            //[self listenDeviceWithPath:@"1" session:session0];
            //[self startBackupDeviceCommandWithSession:session0];
            //[self sendRestartCommandWithSession:session0];
            //[self getFeaturesWithSessionForCreate:session0];
            [self handleDeviceResponseString:responseString session: session0];
        } else {
            NSLog(@"Failed to send ButtonAck command. Status code: %ld", (long)httpResponse.statusCode);
            //            [self listenDeviceWithPath:@"1" session:session1];
        }
    }];
}

- (void)getFeaturesWithSessionForPin:(NSString *)session0 {
    NSString *hexString = @"003700000000";
    NSData *bodyData = [hexString dataUsingEncoding:NSUTF8StringEncoding];
    NSString *urlString = [NSString stringWithFormat:@"http://127.0.0.1:21325/call/%@", session0];
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/octet-stream" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:bodyData];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (!error) {
            NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
            if (httpResponse.statusCode == 200) {
                NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                NSLog(@"Raw Response String: %@", responseString); // This will show the exact content of the response
                NSString *substring = [responseString substringFromIndex:12];
                NSData *featuresData = [HexConverter dataFromHexString:substring];
                NSError *parseError = nil;
                Features *features = [Features parseFromData:featuresData error:&parseError];
                if (parseError) {
                    NSLog(@"Failed to parse features: %@", parseError.localizedDescription);
                } else {
                    NSLog(@"Received Features: %@", features);
                        
                        if (features) {
                                if (features.hasPinProtection && !features.pinProtection) {
                                    NSLog(@"PIN protection is enabled on the device.");
                                    [self startChangePin:session0];
                                } else {
                                    NSLog(@"PIN protection is not enabled on the device.");
                                }
                            }
                }
            }
        } else {
            NSLog(@"Error sending PIN: %@", error.localizedDescription);
        }
    }];
    [dataTask resume];
}

- (void)startChangePin:(NSString *)session0 {
    
    NSString *hexString = @"0004000000020800"; // ResetDevice command
    NSData *bodyData = [hexString dataUsingEncoding:NSUTF8StringEncoding];
    [NetworkCenter startBackupDeviceCommandWithSessionWithBody:bodyData session:session0 completion:^(NSData *data, NSError *error, NSURLResponse *response) {
        if (error) {
            NSLog(@"Error sending ResetDevice command: %@", error.localizedDescription);
            return;
        }
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
        if (httpResponse.statusCode == 200) {
            NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"Raw Response String: %@", responseString); // This will show the exact content of the response
            //NSString *substring = [responseData substringFromIndex:12];
            // Handle the response, possibly send ButtonAck
            [self handleDeviceResponseString:responseString session: session0];
            
        } else {
            NSLog(@"Failed to send ResetDevice command. Status code: %ld", (long)httpResponse.statusCode);
        }
    }];
}

-(NSData *)generateSecureRandomData:(NSInteger )length{
    NSMutableData *randomData = [[NSMutableData alloc] initWithLength:length];
    if (SecRandomCopyBytes(kSecRandomDefault, length, randomData.mutableBytes) != errSecSuccess) {
        NSLog(@"Error generating secure random bytes");
        return nil;
    }
    return randomData;
}


+(NSData *)getMessageResponse:(NSData *)data
                        url:(NSURL *)url;
{
    return [NetworkManager sendRequestWithURL2: url body:data];
}

@end
